import common.CommonDemo;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

public class Confirm {

    private CommonDemo commonDemo;
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeTest
    public void setUp() throws Exception{
        commonDemo = new CommonDemo();
        driver = commonDemo.getChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(2));
    }
    @Test
    public void test() throws Exception{
        String url = "src/main/resources/Confirm.html";
        String filepath = commonDemo.getUserDir(url);
        driver.get(filepath);
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By.name("confirm"));
        wait.until(ExpectedConditions.elementToBeClickable(element)).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String content = alert.getText();
        System.out.println(content);
        Thread.sleep(2000);
        alert.dismiss();
    }
    @AfterTest
    public void tearDown() throws Exception{
        Thread.sleep(2000);
        driver.quit();
    }
}
